﻿using Newtonsoft.Json;
using PH_Zu_DataUpload_Auto;
using PH_Zu_DataUpload_Auto.Properties;
using Sunny.UI;
using System;
using System.Configuration;
using System.Net.Http;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PH_Zy_DataUpload_Auto
{
    public partial class Login : UIForm
    {
        public Login()
        {
            InitializeComponent();
        }

        Boolean loginFlag = true;

        private async void uiButton1_Click(object sender, EventArgs e)
        {
            if (loginFlag)
            {

                try
                {
                    loginFlag = false;
                    // 验证用户名和密码
                    if (await CheckLogin(U.Text, P.Text))
                    {
                        // 创建并显示主窗口
                        Main mainForm = new Main();
                        mainForm.Show();

                        // 隐藏窗体
                        this.Hide();

                    }
                }
                catch (Exception ex)
                {
                    Logger.Error(ex);
                    MessageBox.Show(ex.InnerException.Message);
                }
                finally
                {
                    loginFlag = true;
                }
            }
            else
            {
                return;
            }


        }

        private async Task<Boolean> CheckLogin(string text, string password)
        {
            if (string.IsNullOrEmpty(password))
                return false;
            if (string.IsNullOrEmpty(password))
                return false;
            MesHttp mesHttp = new MesHttp();
            HttpResponseMessage responseMessage = await mesHttp.login(text, password);
            String result = await responseMessage.Content.ReadAsStringAsync();
            MesHttpResult mesHttpResult = JsonConvert.DeserializeObject<MesHttpResult>(result);
            if (!mesHttpResult.status)
            {
                FlaseHttpResult fHttpResult = JsonConvert.DeserializeObject<FlaseHttpResult>(result);
                MessageBox.Show(fHttpResult.message);
                return false;
            }
            TrueHttpResult tHttpResult = JsonConvert.DeserializeObject<TrueHttpResult>(result);

            // 设置配置文件中的键值对
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            Logger.Info("登录成功【" + tHttpResult.message.realname + "】");
            config.AppSettings.Settings["User"].Value = tHttpResult.message.realname;
            config.Save(ConfigurationSaveMode.Modified);

            // 重新加载配置文件以使更改生效
            ConfigurationManager.RefreshSection("appSettings");

            return true;
        }

        // FormClosing 事件处理程序
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            // 处理窗体关闭操作
            Application.Exit();
        }

        private void P_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                uiButton1_Click(null, null);
                e.Handled = true;
            }

        }

    }
}
